use axum::Json;
use serde_json::Value;

use crate::{code, mdb::gridfs::gridfs_delete, query};

// 删除指定桶中的指定文件
pub async fn one(Json(body):Json<Value>) -> Result<Json<Value>, Json<Value>>{
    let id = query::str(&body, "id")?;
    let bucket = query::str(&body, "bucket")?;
    let _ = gridfs_delete::one(&id, &bucket).await?;
    code::ok()
}
pub async fn many(Json(body):Json<Value>) -> Result<Json<Value>, Json<Value>>{
    let id = query::vec(&body, "id")?;
    let bucket = query::str(&body, "bucket")?;

    if id.len() == 1 {
        let id = id.get(0).unwrap().as_str().unwrap_or_default();
        let _ = gridfs_delete::one(id, &bucket).await?;
            code::ok()
    }else{
        let _ = gridfs_delete::many(id, &bucket).await?;
        code::ok()
    }
}
pub async fn many_auto(Json(body):Json<Value>) -> Result<Json<Value>, Json<Value>>{
    let bucket = query::str(&body, "bucket")?;
    if let Some(f) = &body["id"].as_str() {
        let _ = gridfs_delete::one(f, &bucket).await?;
        code::ok()
    }else{
        let id = query::vec(&body, "id")?;
        let _ = gridfs_delete::many(id, &bucket).await?;
        code::ok()
    }
}
